limsResult.editPage = {

    getRunGrid: function () {
        var _this = this;
        return {
            type: 'grid',
            id: 'run-list-grid',
            checkOnActive: true,
            service: 'limsRunServiceImpl',
            url: limsResult.runUrl + '/queries/raw',
            columns: limsResult.getRunGridColumns(),
            requestData: {
                analystId_EQ: workspace.user.id,
                status_EQ: 'logged',
                //createdByOrgId_EQ: workspace.user.ext$.loginorgid
            },
            page: true,
            onLoadSuccess: function (rows) {
                if (Gikam.isNotEmpty(rows)) {
                    this.activeRowByIndex(0)
                }
            },
            onRowActive: function (index, row) {//批次的行激活
            },
            toolbar: [{
                type: 'button',
                text: 'GIKAM.TEXT.RUN.INFO',
                icon: 'select',
                onClick: function () {
                    _this.updateRunInfo();
                }
            }, {
                type: 'button',
                text: 'GIKAM.WORKFLOW.BUTTON.SUBMIT',
                icon: 'submit', color: 'success',
                onClick: function () {
                    _this.submitRun();
                }
            }, {
                type: 'button',
                text: 'MODULE.BUSINESS.RESULT.BUTTON.RETURNBACK',
                icon: 'revert',
                onClick: function () {
                    _this.returnBack();
                }
            }, {
                type: 'button',
                text: 'MODULE.BUSINESS.RESULT.BUTTON.HAND_OVER',
                icon: 'export',
                onClick: function () {
                    _this.handOver()
                }
            }, {
                type: 'button',
                text: 'GIKAM.BUTTON.EXPORTRUNTASK',
                icon: 'export',
                onClick: function () {
                    _this.exportRunTaskExcelNew();
                }
            }, {
                type: 'button',
                text: 'GIKAM.BUTTON.IMPORTRUNTASK',
                icon: 'upload',
                onClick: function () {
                    _this.importRunTaskExcelNew();
                }
            },],
            onCellClick: function (field, row) {
                field === 'runCode' && row.id && _this.loadRunOrderTaskPage(row);
                field === 'methodDesc' && _this.loadMethodDetailPage(row);
            },
        }
    },

    exportRunTaskExcelNew: function () {
        var _this = this;
        var selections = Gikam.getComp('run-list-grid').getSelections();
        if (Gikam.isEmpty(selections)) {
            Gikam.alert('MODULE.BUSINESS.RESULT.TIP.CHOOSE_ONE_RUN')
            return
        }
        selections.forEach(r => {
            Gikam.getText(IFM_CONTEXT + '/core/module/item/files/template-urls' + Gikam.param({
                templateCode: 'LimsRunTaskListExportTemplate',
                service: 'LimsRunTaskListExportTemplate',
                id: r.id
            })).done(function (url) {
                Gikam.download(url);
            })
        })
    },

    importRunTaskExcelNew: function () {
        var _this = this;
        var runGrid = Gikam.getComp('run-list-grid')
        var activedRow = runGrid.getActivedRow()
        if (Gikam.isEmpty(activedRow)) {
            return
        }
        Gikam.create('simpleUploader', {
            id: 'run-list-info-import',
            dbTable: 'T_LIMS_RUN',
            bizId: activedRow.id,
            autoClose: true,
            multiple: false,
            onAfterClose: function (fields) {
                if (Gikam.isNotEmpty(fields)) {
                    // 获取固定列的数量：
                    var fixedColumn = limsResult.getRunOrderTaskGridColumns()
                    var size = fixedColumn.length + 2
                    workspace.window.showMask()
                    var url = Gikam.printf(IFM_CONTEXT + '/core/module/item/files/{id}/action/import/' + 'LimsRunTaskListImportFileFunction', {
                        id: fields[0].id
                    }) + Gikam.param({
                        // fixedColumnSize: size,
                        // runCode: activedRow.runCode
                    });
                    Gikam.post(url).done(function (req) {
                        runGrid.refresh()
                        if (req.message){
                            Gikam.alert(req.message)
                        }
                        if (req.url){
                            Gikam.download(req.url);
                        }
                    }).always(function () {
                        workspace.window.closeMask()
                    });
                }

            }
        });
    },

    handOver: function () {
        var _this = this
        var runListGrid = Gikam.getComp('run-list-grid')
        var selections = runListGrid.getSelections()
        if (Gikam.isEmpty(selections)) {
            Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM')
            return
        }
        var testIds = [selections[0].testId]
        selections.forEach(r => {
            if (!testIds.includes(r.testId)) {
                testIds.push(r.testId)
            }
        })
        if (testIds.length > 1) {
            Gikam.alert('MODULE.BUSINESS.RESULT.TIP.ONLY_CHOOSE_ONE_TEST')
            return
        }
        var testerIds = '';
        Gikam.getJsonSync(Gikam.printf(IFM_CONTEXT + '/secure/basemodule/coredata/tests/{id}', {
            id: testIds[0]
        })).done(function (data) {
            testerIds = data.testerId
        })
        Gikam.createLimsModal({
            title: 'T_LIMS_ORDER_TASK.BUTTON.ASSIGNMENT',
            height: '200',
            width: '400',
            onConfirm: function () {
                var form = Gikam.getComp('result-edit-run-handover-form')
                if (form.validate()) {
                    var data = form.getData()
                    if (Gikam.isEmpty(data)) {
                        return
                    }
                    let url = limsResult.runUrl + '/action/result-edit/hand-over'
                    workspace.window.showMask()
                    Gikam.postText(url, Gikam.getJsonWrapper(data, [null, selections])).done(req => {
                        runListGrid.refresh()
                    }).always(function () {
                        workspace.window.closeMask()
                        Gikam.getLastModal().close()
                    })
                }
            },
            m: {
                type: 'form',
                id: 'result-edit-run-handover-form',
                columns: 1,
                titleWidth: 80,
                fields: [{
                    field: 'testerId',
                    title: 'T_LIMS_ORDER_TASK.TESTER',
                    type: 'select',
                    category: 'user',
                    validators: ['notEmpty'],
                    firstBlank: false,
                    search: true,
                    param: {
                        status_EQ: 'activated',
                        id_IN: testerIds
                    },
                    onChange: function (field, value, oldvalue, index, text) {
                        this.setData({
                            tester: text
                        })
                    }
                },],
            }
        })
    },

    loadRunOrderTaskPage: function (row) {
        workspace.window.load(limsResult.baseUrl + '/page/run-order-task-detail' + Gikam.param({
            id: row.id,
            runCode: row.runCode
        }))
    },

    loadMethodDetailPage: function (row) {
        Gikam.create('modal', {
            //title: 'T_LIMS_METHOD_VERSION.SERVICE_NAME',
            url: IFM_CONTEXT + '/secure/basemodule/coredata/methods/page/detail-list' + Gikam.param({
                id: row.methodCode
            })
        });
    },

    updateRunInfo: function () {
        let runGrid = Gikam.getComp('run-list-grid');
        let selections = runGrid.getSelections();
        if (Gikam.isEmpty(selections)) {
            Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM');
            return;
        }
        Gikam.createLimsModal({
            title: 'GIKAM.TEXT.RUN.INFO',
            height: 240,
            width: 400,
            onConfirm: function () {
                var form = Gikam.getComp('confirm-form');
                if (form.validate()) {
                    let data = form.getData();
                    var chooseData = selections.map(function (item) {
                        return {
                            id: item.id,
                            roomNo: data.roomNo,
                            temperature: data.temperature,
                            dampness: data.dampness,
                            // pressureTo: data.pressureTo,
                            // pressureFrom: data.pressureFrom,
                        }
                    });
                    workspace.window.showMask();
                    Gikam.put(limsResult.runUrl + '/instant', Gikam.getJsonWrapper(null, [null, chooseData])).done(function () {
                        runGrid.refresh();
                    }).always(function () {
                        workspace.window.closeMask();
                        Gikam.getLastModal().close();
                    })
                }
            },
            m: {
                type: 'form',
                id: 'confirm-form',
                titleWidthAuto: true,
                fields: limsResult.getBatchInfoFormFields(),
                columns: 1,
            },
        })
    },

    ckeditorResult: function (analyteId, orderTaskId) {

    },

    submitRun: function () {
        let grid = Gikam.getComp('run-list-grid');
        let selections = grid.getSelections();
        if (Gikam.isEmpty(selections)) {
            Gikam.alert('GIKAM.TIP.CHOOSE_ONE_ITEM');
            return;
        }
        Gikam.confirm('GIKAM.MODAL.SUBMIT', 'GIKAM.TIP.SUBMIT', function () {
            workspace.window.showMask();
            Gikam.postText(limsResult.runUrl + '/action/submit',
                Gikam.getJsonWrapper(null, [null, selections])).done(function (result) {
                grid.refresh();
            }).always(function () {
                workspace.window.closeMask();
            });
        })
    },

    returnBack: function () {
        let grid = Gikam.getComp('run-list-grid');
        let selections = grid.getSelections();
        if (Gikam.isEmpty(selections)) {
            Gikam.alert('GIKAM.TIP.CHOOSE_ONE_ITEM');
            return;
        }
        Gikam.createLimsModal({
            title: 'MODULE.BUSINESS.RESULT.BUTTON.RETURNBACK',
            height: 220,
            width: 380,
            onConfirm: function () {
                var form = Gikam.getComp('confirm-form');
                if (form.validate()) {
                    let data = form.getData();
                    workspace.window.showMask();
                    Gikam.postText(limsResult.orderTaskUrl + '/back/scheduling',
                        Gikam.getJsonWrapper(data, [null, selections])).done(function (result) {
                        grid.refresh();
                    }).always(function () {
                        Gikam.getLastModal().close();
                        workspace.window.closeMask();
                    });
                }
            },
            m: {
                type: 'form',
                id: 'confirm-form',
                fields: [{
                    field: 'reason',
                    title: 'T_LIMS_RUN.REJECTREASON',
                    validators: ['notEmpty'],
                    type: 'textarea',
                    height: 100,
                }],
                columns: 1,
            }
        })
    },

    create: function () {
        var _this = this;
        Gikam.create('layout', {
            id: 'lims-result-edit-list-layout',
            renderTo: workspace.window.$dom,
            center: {
                items: [this.getRunGrid()]
            }
        });
    },

    init: function () {
        this.create();
    }
};
